package com.example.studentmanager;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AllStudent extends AppCompatActivity {
    EditText gender, adress, phone, chenji;
    Button btntoupate;
    private LinearLayout linearLayout;
    StudentDB studentDB;
    private List<Student> list;
    TextView count;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_student);
        gender = findViewById(R.id.gender);
        adress = findViewById(R.id.adress);
        phone = findViewById(R.id.phone);
        chenji = findViewById(R.id.chengji);
        btntoupate = findViewById(R.id.btntoupate);
        count = findViewById(R.id.count);

        linearLayout = findViewById(R.id.linearly);

        btntoupate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AllStudent.this, UpdateStudent.class);
                intent.putExtra("xx", "学号： ");
                startActivity(intent);
            }
        });


    }

    protected void getlist() {

        linearLayout.removeAllViews();

        for (int i = 0; i < list.size(); i++) {
            // Inflate a new button view

            View view = LayoutInflater.from(this).inflate(R.layout.studentdescrip, linearLayout, false);

            TextView textView = view.findViewById(R.id.text0);
            TextView textView1 = view.findViewById(R.id.text1);
            TextView textView2 = view.findViewById(R.id.text2);
            TextView textView3 = view.findViewById(R.id.text3);
            TextView textView4 = view.findViewById(R.id.text4);
            TextView textView5 = view.findViewById(R.id.text5);


            textView.setText("学生： " + (i + 1));
            textView1.setText("学号： " + list.get(i).xuehao);
            textView2.setText("性别： " + list.get(i).gender);
            textView3.setText("地址： " + list.get(i).address);
            textView4.setText("电话： " + list.get(i).phone);
            textView5.setText("成绩： " + list.get(i).chengji);


            linearLayout.addView(view);


            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(AllStudent.this)
                            .setTitle("【修改】/【删除】学号为：" + textView1.getText().toString().substring(4) + "的学生信息？")
                            .setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(AllStudent.this, UpdateStudent.class);
                                    intent.putExtra("xx", textView1.getText().toString());
                                    startActivity(intent);
                                }
                            }).setNegativeButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(AllStudent.this, DeleteStudent.class);
                                    intent.putExtra("yy", textView1.getText().toString());
                                    startActivity(intent);
                                }
                            });
                    builder.create().show();


                    return true;
                }
            });

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v("update", "onResume2");
        studentDB = StudentDB.getStudentDB(AllStudent.this);
        list = studentDB.queryall();
        count.setText("学生人数：" + list.size());
        getlist();

    }


}